package main

import (
	"github.com/julienschmidt/httprouter"
	"net/http"
	_ "gitee.com/Cookie_XiaoD/video_server/api/dbops"
)

func RegisterHandlers() *httprouter.Router{


	router:=httprouter.New()

	router.POST("/user",CreateUser)

	router.POST("/user/:user_name",Login)

	router.GET("/user/:uid",GetUserInfo)

	router.GET("/video/:vid/comments",ListComment)



	return router

}

func main() {
	r:=RegisterHandlers()
	m:=newAuthMiddleware(r)

	//这是一个阻塞方法
	http.ListenAndServe(":8000",m)
}


//鉴权 中间件，用于拦截非法请求
//通过实现ServeHTTP方法
//实现http.Handler接口
type authMiddleware struct {
	router *httprouter.Router
}

func newAuthMiddleware(r *httprouter.Router) http.Handler{
	m:=&authMiddleware{
		router:r,
	}
	return m
}

func (m *authMiddleware)ServeHTTP(w http.ResponseWriter,r *http.Request){
	//检查session
	validateUserSession(r)


	m.router.ServeHTTP(w,r)
}